//
//  WeeklyLetterContentView.swift
//  imprint
//
//  Created by 小柠檬🍋 on 2025/2/25.
//

import SwiftUI
import Kingfisher

//来信内容弹窗试图
struct WeeklyLetterContentView: View {
    var bean : WeeklyLetterBean
    @State private var visibleCharacters = 0
    @State private var timer: Timer?
    
    var body: some View {
        PopupBottomView(title: "") {
            VStack(alignment:.leading,spacing: 10) {
                HStack(spacing : 5) {
                    Image(.homeIconMail)
                        .resizable()
                        .renderingMode(.template)
                        .foregroundColor(themeManager.themeColor)
                        .frame(width: 20, height: 20)
                    Text("来信时间：")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgNormalText3)
                    Text(TimeUtils.getDate(time: bean.time, format: "yyyy年M月d日 HH:mm"))
                        .font(.system(size: 14))
                        .foregroundStyle(.bgNormalText3)
                }.frame(maxWidth: .infinity,alignment: .topLeading)
                HStack(spacing : 5){
                    Text("写信人：")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgNormalText3)
                    KFImage(URL(string: bean.avatar.formatCdn()))
                        .resizable()
                        .placeholder {
                            Image(.appDefaultLogo)
                                .resizable()
                                .aspectRatio(contentMode: .fill)
                                .padding(4)
                                .background(.bgItemGray)
                        }
                        .aspectRatio(contentMode: .fill)
                        .frame(width: 20,height: 20)
                        .clipShape(Circle())
                    Text(bean.name ?? "")
                        .font(.system(size: 14))
                        .foregroundStyle(.bgNormalText3)
                }.frame(maxWidth: .infinity,alignment: .topLeading)
                ScrollView {
                    Text(String(Array(bean.content ?? "").prefix(visibleCharacters)))
                        .font(.system(size: 14))
                        .foregroundStyle(themeManager.themeColor)
                        .padding(.top, 20)
                }.scrollIndicators(.hidden)
            }.frame(maxHeight: .infinity,alignment: .topLeading)
        } .onAppear { startAnimation() }
            .onDisappear {
                timer?.invalidate()
                AudioPlayerManager.shared.pause()
            }
    }
    
    private func startAnimation() {
        if bean.read {
            visibleCharacters = bean.content?.count ?? 0
        } else {
            AudioPlayerManager.shared.playLocal(forResource:"typewriting",ofType:"mp3", looper : true)
            timer?.invalidate()
            visibleCharacters = 0
            let characters = Array(bean.content ?? "")
            timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { timer in
                if visibleCharacters < characters.count {
                    visibleCharacters += 1
                } else {
                    timer.invalidate()
                    AudioPlayerManager.shared.pause()
                }
            }
        }
    }
}

#Preview {
    WeeklyLetterContentView(bean: WeeklyLetterBean())
}
